package com.adcc.ags.agent.healthcheck;

import com.adcc.utility.log.Log;
import com.codahale.metrics.health.HealthCheck;
import com.fasterxml.jackson.databind.ObjectMapper;
import jersey.repackaged.com.google.common.collect.Maps;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;

import java.util.Map;

/**
 * 硬盘健康检查
 */
public class DiskHealthCheck extends HealthCheck {

    @Override
    protected Result check() {
        Sigar sigar = null;
        try{
            sigar = new Sigar();
            Map<String,Double> map = Maps.newHashMapWithExpectedSize(5);
            FileSystem[] fileSystemList = sigar.getFileSystemList();
            for(int i = 0;i < fileSystemList.length;i++){
                String strDirName = fileSystemList[i].getDirName();
                int intType = fileSystemList[i].getType();
                if(intType == 2){
                    FileSystemUsage usage = sigar.getFileSystemUsage(strDirName);
                    double dUsePercent = usage.getUsePercent() * 100;
                    map.put(strDirName,dUsePercent);
                }
            }
            return Result.healthy(new ObjectMapper().writeValueAsString(map));
        }catch (Exception ex){
            Log.error(DiskHealthCheck.class.getName(),"check() error",ex);
            return Result.unhealthy(ex.fillInStackTrace());
        }finally {
            if(sigar != null){
                sigar.close();
            }
        }
    }
}
